package com.link.base.base.chiper.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.service.BasicServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.base.base.chiper.model.ChiperRead;
import com.link.base.base.chiper.service.ChiperReadService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 郑洪达
 * @date: 2018-12-20
 * @comments:
 */
@Controller
@RequestMapping("/link/chiperRead")
public class ChiperReadController extends BasicController<ChiperRead> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ChiperReadService chiperReadService;

    @Override
    public BasicService<ChiperRead> getBasicService() throws Exception {
        return chiperReadService;
    }

    /**
     * 读操作隐藏数据插入（LNK_CHIPER_READ 表记录查询时候是否隐藏或者部分隐藏）
     *
     * @author 郑洪达
     * @date 2019/12/12
     * @param chiperRead 隐藏对象，包含隐藏类，字段，是否全部隐藏等信息
     * @return
     */ 
    @RequestMapping(value = "/saveChiperRead", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveChiperRead(@JsonParam ChiperRead chiperRead, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (!chiperReadService.checkChiper(chiperRead)) {
                result.put("success", false);
                result.put("result", "找不到该属性");
            } else if ("Y".equals(chiperRead.getHideFlag())
                    && !chiperReadService.checkHidePart(chiperRead)) {
                //判断字符串隐藏部分位置输入合法性
                result.put("success", false);
                result.put("result", "字段隐藏位置:开始位置大于等于1,结束位置不可小于开始位置");
            } else {
                if (null == chiperRead.getId()) {
                    chiperRead.setId(keyGenerateService.keyGenerate());
                }
                if ("N".equals(chiperRead.getHideFlag())) {
                    chiperRead.setStartIndex("0");
                    chiperRead.setEndIndex("0");
                }
                //特定字段不可加密
                String fieldName = chiperRead.getFieldName();
                if ("id".equals(fieldName) || "created".equals(fieldName) || "createdBy".equals(fieldName)
                        || "lastUpdated".equals(fieldName) || "lastUpdatedBy".equals(fieldName) || "corpid".equals(fieldName)) {
                    result.put("success", false);
                    result.put("result", "该字段不可加密");
                } else {
                    chiperReadService.upsert(chiperRead);
                    ChiperRead chiperReadResult = chiperReadService.queryById(chiperRead);
                    result.put("success", true);
                    result.put("newRow", chiperReadResult);
                }
            }
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            if ("唯一性冲突".equals(e.getMessage())) {
                result.put("success", false);
                result.put("result", "该方法和属性已存在");
            } else {
                result.put("success", false);
                result.put("result", "找不到该类或字段隐藏位置输入有误(请输入整数)");
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", "添加失败");
        }
        return result;
    }

    /**
     * 清除隐藏表在Redis中的数据，并且重新加载
     *
     * @author 郑洪达
     * @date 2019/12/12
     * @param chiperRead 隐藏对象信息
     * @return
     */
    @RequestMapping(value = "/clearChiperReadCache", method = RequestMethod.POST)
    @ResponseBody
        public Map<String, Object> clearChiperReadCache(@JsonParam ChiperRead chiperRead, HttpSession session, HttpServletRequest request,
                                                        HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            chiperReadService.clearCache(chiperRead);
            result.put("success", true);
            result.put("result", "清除缓存成功");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
